package kd.bos.kdec.pincar;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.EventObject;
import java.util.HashMap;
import java.util.Map;

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.form.ShowType;
import kd.bos.form.control.Control;
import kd.bos.form.control.Label;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.servicehelper.org.OrgServiceHelper;
import kd.bos.servicehelper.user.UserServiceHelper;

/**
 * 拼车我的 页面
 * 
 * @author zhangyizhe
 *
 */
public class PinCarForMobMyPage extends AbstractFormPlugin {

	private String[] buttons = new String[] { "pincarclick", "dingbill", "myclick", "setting", "quanclick" };

	@Override
	public void registerListener(EventObject e) {
		super.registerListener(e);
		this.addClickListeners(this.buttons);
	}

	@Override
	public void click(EventObject evt) {
		super.click(evt);
		Control control = (Control) evt.getSource();
		String key = control.getKey();
		Map<String, Object> formId = new HashMap<>();
		if (StringUtils.equals("dingbill", key)) {// 行程单列表
			formId.put("formId", "kdec_pincarbilllist");
			this.showForm(formId);
		} else if (StringUtils.equals("pincarclick", key)) {// 首页跳转
			formId.put("formId", "kdec_pincar");
			this.showForm(formId);
		} else if (StringUtils.equals("setting", key)) {// 设置
			formId.put("formId", "kdec_pincar_setting_p");
			this.showForm(formId);
		} else if (StringUtils.equals("quanclick", key)) {// 圈子跳转
			formId.put("formId", "kdec_pincar_circle");
			this.showForm(formId);
		}
	}

	@Override
	public void afterBindData(EventObject e) {
		super.afterBindData(e);
		// 查询当前用户的完成行程最新一条
		IFormView view = this.getView();
		long userId = Long.valueOf(RequestContext.get().getUserId());
		QFilter filter = new QFilter("creator", QCP.equals, userId);
		filter.and(new QFilter("orderstatus", QCP.equals, "3"));
		DynamicObjectCollection collection = QueryServiceHelper.query("kdec_pincarbill",
				"id,startplace,endplace,traveldate,modifytime", filter.toArray(), "modifytime desc", 1);
		if (null != collection && collection.size() != 0) {
			DynamicObject billobj = collection.get(0);
			Label label = this.getControl("startplace");
			label.setText(billobj.get("startplace").toString());
			Label label2 = this.getControl("endplace");
			label2.setText(billobj.get("endplace").toString());
			Date date = (Date) billobj.get("traveldate");
			DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String date1 = sdf.format(date);
			Label label3 = this.getControl("tarveldate");
			label3.setText(date1);
		} else {
			view.setVisible(true, "ipincardata");
			view.setVisible(false, "sed");
		}
	}

	/**
	 * 页面跳转通用方法
	 * 
	 * @param formId
	 *            页面标识
	 */
	private void showForm(Map<String, Object> formId) {
		FormShowParameter showParameter = FormShowParameter.createFormShowParameter(formId);
		showParameter.getOpenStyle().setShowType(ShowType.Floating);
		this.getView().showForm(showParameter);
	}
}
